package com.ruoyi.ade.controller;

import com.ruoyi.ade.domain.AdeKnowledgeBase;
import com.ruoyi.ade.llm.model.AdeRetrivedResponse;
import com.ruoyi.ade.model.AdeSearchRequest;
import com.ruoyi.ade.service.IAdeKnowledgeBaseService;
import com.ruoyi.ade.service.IAdeKnowledgeIndexService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ADE Knowledge BaseController
 *
 * @author wxh
 * @date 2025-07-07
 */
@RestController
@RequestMapping("/ade/adebase")
public class AdeKnowledgeBaseController extends BaseController {
    @Autowired
    private IAdeKnowledgeBaseService adeKnowledgeBaseService;

    @Autowired
    private IAdeKnowledgeIndexService adeKnowledgeIndexService;


    /**
     * 检索ADE知识
     * @param request
     * @return
     */
    @PostMapping("/search")
    public AjaxResult search(@RequestBody AdeSearchRequest request){
        try {
            AdeRetrivedResponse response = adeKnowledgeIndexService.search(request);
            return AjaxResult.success(response);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @PutMapping("/execute/{id}")
    public AjaxResult execute(@PathVariable("id") Long id) {
        try {
            adeKnowledgeIndexService.execute(id);
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    /**
     *
     * @param adeKnowledgeBase
     * @return
     */
    @GetMapping("/listAll")
    public AjaxResult listAll(AdeKnowledgeBase adeKnowledgeBase) {
        List<AdeKnowledgeBase> list = adeKnowledgeBaseService.selectAdeKnowledgeBaseList(adeKnowledgeBase);
        return AjaxResult.success(list);
    }
    /**
     * 查询ADE Knowledge Base列表
     */
    @PreAuthorize("@ss.hasPermi('ade:adebase:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdeKnowledgeBase adeKnowledgeBase) {
        startPage();
        List<AdeKnowledgeBase> list = adeKnowledgeBaseService.selectAdeKnowledgeBaseList(adeKnowledgeBase);
        return getDataTable(list);
    }

    /**
     * 导出ADE Knowledge Base列表
     */
    @PreAuthorize("@ss.hasPermi('ade:adebase:export')")
    @Log(title = "ADE Knowledge Base", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdeKnowledgeBase adeKnowledgeBase) {
        List<AdeKnowledgeBase> list = adeKnowledgeBaseService.selectAdeKnowledgeBaseList(adeKnowledgeBase);
        ExcelUtil<AdeKnowledgeBase> util = new ExcelUtil<AdeKnowledgeBase>(AdeKnowledgeBase.class);
        util.exportExcel(response, list, "ADE Knowledge Base数据");
    }

    /**
     * 获取ADE Knowledge Base详细信息
     */
    @PreAuthorize("@ss.hasPermi('ade:adebase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(adeKnowledgeBaseService.selectAdeKnowledgeBaseById(id));
    }

    /**
     * 新增ADE Knowledge Base
     */
    @PreAuthorize("@ss.hasPermi('ade:adebase:add')")
    @Log(title = "ADE Knowledge Base", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdeKnowledgeBase adeKnowledgeBase) {
        return toAjax(adeKnowledgeBaseService.insertAdeKnowledgeBase(adeKnowledgeBase));
    }

    /**
     * 修改ADE Knowledge Base
     */
    @PreAuthorize("@ss.hasPermi('ade:adebase:edit')")
    @Log(title = "ADE Knowledge Base", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdeKnowledgeBase adeKnowledgeBase) {
        return toAjax(adeKnowledgeBaseService.updateAdeKnowledgeBase(adeKnowledgeBase));
    }

    /**
     * 删除ADE Knowledge Base
     */
    @PreAuthorize("@ss.hasPermi('ade:adebase:remove')")
    @Log(title = "ADE Knowledge Base", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(adeKnowledgeBaseService.deleteAdeKnowledgeBaseByIds(ids));
    }
}
